package com.camera2.http;

import android.content.Context;
import android.util.Log;
import com.camera2.local.SharedPrefer;
import org.json.JSONObject;
import java.io.File;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SendImage {
	public static String imagePath;
	private static OkHttpClient client = new OkHttpClient();
	public static String send(Context context, String str1, String str2, String str3) throws Exception {
		String TAG="SendImage";
		String message = null;
		String id = SharedPrefer.Readid(context);
		File imagefile = new File(imagePath);
		System.out.println("图片路径" + imagePath);
		System.out.println("发送图片参数" + str1 + str2 + str3 + id);
		OkHttpClient client = new OkHttpClient();
		MultipartBody.Builder builder = new MultipartBody.Builder()
				.setType(MultipartBody.FORM)
				.addFormDataPart("reason", str2)
				.addFormDataPart("injury", str1)
				.addFormDataPart("infected", str3)
				.addFormDataPart("userId", id)
				.addFormDataPart("jpg", imagePath, RequestBody.create(MediaType.parse("image/png"), imagefile));

		MultipartBody requestBody = builder.build();
		Request request = new Request.Builder()
				.url(GlobalUrl.getUploadFileUrl())
				.post(requestBody)
				.build();
		Response response = client.newCall(request).execute();
		String result = response.body().string();
		System.out.println("图片结果" + result);
		JSONObject jsonObj = new JSONObject(result);
		message = jsonObj.getString("msg");

		return message;
	}

}
